﻿Imports System.Data.OleDb

Public Class WatchList


    Public dog As WatchDog
    Public isSaved As Boolean = False
    Public iType As Integer


    Private Sub WatchList_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.Text = dog.dogName
        Me.txtMonitorName.Text = dog.dogName
        Me.txtWatchFolder.Text = dog.watchGate

        If iType = WatchEnemy.EnemyType.Add Then
            Me.txtMonitorName.Enabled = True
        Else
            Me.txtMonitorName.Enabled = False
        End If

        For Each bone As Bone In dog.boneList


            Dim dgr As New DataGridViewRow
            'dgr = Me.DataGridView1.Rows.Item(addIndex)

            ' ファイル名
            Dim cellFileName As New DataGridViewTextBoxCell()
            cellFileName.Value = bone.FileName
            dgr.Cells.Add(cellFileName)

            ' 新規監視
            Dim cellNeedMonitorCreate As New DataGridViewCheckBoxCell()
            cellNeedMonitorCreate.Value = bone.NeedMonitorCreate
            dgr.Cells.Add(cellNeedMonitorCreate)

            Dim cellCreateMessage As New DataGridViewTextBoxCell()
            cellCreateMessage.Value = bone.CreateMessage
            dgr.Cells.Add(cellCreateMessage)

            ' リネーム監視
            Dim cellNeedMonitorRename As New DataGridViewCheckBoxCell()
            cellNeedMonitorRename.Value = bone.NeedMonitorRename
            dgr.Cells.Add(cellNeedMonitorRename)

            Dim cellRenameMessage As New DataGridViewTextBoxCell()
            cellRenameMessage.Value = bone.RenameMessage
            dgr.Cells.Add(cellRenameMessage)

            ' 削除監視
            Dim cellNeedMonitorDelete As New DataGridViewCheckBoxCell()
            cellNeedMonitorDelete.Value = bone.NeedMonitorDelete
            dgr.Cells.Add(cellNeedMonitorDelete)

            Dim cellDeleteMessage As New DataGridViewTextBoxCell()
            cellDeleteMessage.Value = bone.DeleteMessage
            dgr.Cells.Add(cellDeleteMessage)

            ' 更新監視
            Dim cellNeedMonitorChange As New DataGridViewCheckBoxCell()
            cellNeedMonitorChange.Value = bone.NeedMonitorChange
            dgr.Cells.Add(cellNeedMonitorChange)

            Dim cellChangeMessage As New DataGridViewTextBoxCell()
            cellChangeMessage.Value = bone.ChangeMessage
            dgr.Cells.Add(cellChangeMessage)

            Me.DataGridView1.Rows.Add(dgr)
        Next

    End Sub


    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        If "".Equals(Me.txtMonitorName.Text) Then
            MessageBox.Show("Monitor name can not use a empty name!!!", "error!!!")
            Return
        End If

        If "監視くん".Equals(Me.txtMonitorName.Text) Then
            MessageBox.Show("Monitor name can not use 監視くん!!!", "error!!!")
            Return
        End If

        If (Not "".Equals(MainModule.Read(Me.txtMonitorName.Text, "WATCH_GATE")) And Me.iType = WatchEnemy.EnemyType.Add) Then
            MessageBox.Show("Can not use this exsits monitor name!!!", "error!!!")
            Return
        End If


        If "".Equals(Me.txtWatchFolder.Text) Then
            MessageBox.Show("Monitor directory can not use a empty path!!!", "error!!!")
            Return
        End If

        If Me.DataGridView1.Rows.Count = 0 Then
            MessageBox.Show("Monitor file can not be empty!!!", "error!!!")
            Return
        End If

        Dim listSave As New ArrayList

        For Each dgr As DataGridViewRow In Me.DataGridView1.Rows

            Dim cells As DataGridViewCellCollection
            cells = dgr.Cells

            If Not "".Equals(Trim(cells(0).Value)) Then
                Dim bone As New Bone

                ' ファイル名
                bone.FileName = cells(0).Value
                ' 新規監視
                bone.NeedMonitorCreate = IIf(cells(1).Value = True, "TRUE", "FALSE")
                bone.CreateMessage = cells(2).Value
                ' Re名監視
                bone.NeedMonitorRename = IIf(cells(3).Value = True, "TRUE", "FALSE")
                bone.RenameMessage = cells(4).Value
                ' 削除監視
                bone.NeedMonitorDelete = IIf(cells(5).Value = True, "TRUE", "FALSE")
                bone.DeleteMessage = cells(6).Value
                ' 変更監視
                bone.NeedMonitorChange = IIf(cells(7).Value = True, "TRUE", "FALSE")
                bone.ChangeMessage = cells(8).Value

                listSave.Add(bone)
                bone = Nothing
            End If
        Next
        dog.boneList = listSave
        dog.dogName = Me.txtMonitorName.Text
        dog.watchGate = Me.txtWatchFolder.Text

        MainModule.Save(dog)
        MessageBox.Show("Monitoring list has been saved!", "Success!!!")
        Me.isSaved = True
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        Dim we As New WatchEnemy
        we.type = WatchEnemy.EnemyType.Add
        we.ShowDialog()
        If Not "".Equals(we.bone.FileName) And Not we.bone.FileName Is Nothing Then

            Dim dgr As New DataGridViewRow
            'dgr = Me.DataGridView1.Rows.Item(addIndex)

            ' ファイル名
            Dim cellFileName As New DataGridViewTextBoxCell()
            cellFileName.Value = we.bone.FileName
            dgr.Cells.Add(cellFileName)

            ' 新規監視
            Dim cellNeedMonitorCreate As New DataGridViewCheckBoxCell()
            cellNeedMonitorCreate.Value = we.bone.NeedMonitorCreate
            dgr.Cells.Add(cellNeedMonitorCreate)

            Dim cellCreateMessage As New DataGridViewTextBoxCell()
            cellCreateMessage.Value = we.bone.CreateMessage
            dgr.Cells.Add(cellCreateMessage)

            ' リネーム監視
            Dim cellNeedMonitorRename As New DataGridViewCheckBoxCell()
            cellNeedMonitorRename.Value = we.bone.NeedMonitorRename
            dgr.Cells.Add(cellNeedMonitorRename)

            Dim cellRenameMessage As New DataGridViewTextBoxCell()
            cellRenameMessage.Value = we.bone.RenameMessage
            dgr.Cells.Add(cellRenameMessage)

            ' 削除監視
            Dim cellNeedMonitorDelete As New DataGridViewCheckBoxCell()
            cellNeedMonitorDelete.Value = we.bone.NeedMonitorDelete
            dgr.Cells.Add(cellNeedMonitorDelete)

            Dim cellDeleteMessage As New DataGridViewTextBoxCell()
            cellDeleteMessage.Value = we.bone.DeleteMessage
            dgr.Cells.Add(cellDeleteMessage)

            ' 更新監視
            Dim cellNeedMonitorChange As New DataGridViewCheckBoxCell()
            cellNeedMonitorChange.Value = we.bone.NeedMonitorChange
            dgr.Cells.Add(cellNeedMonitorChange)

            Dim cellChangeMessage As New DataGridViewTextBoxCell()
            cellChangeMessage.Value = we.bone.ChangeMessage
            dgr.Cells.Add(cellChangeMessage)

            Me.DataGridView1.Rows.Add(dgr)

            MessageBox.Show("Target:[ " & we.bone.FileName & " ] has been added!")
        End If
    End Sub


    Private Sub btnUpd_Click(sender As Object, e As EventArgs) Handles btnUpd.Click

        Dim dgr As New DataGridViewRow

        If Me.DataGridView1.SelectedCells.Count = 0 Then
            MessageBox.Show("Is no selected items!")
        Else

            Dim we As New WatchEnemy
            we.type = WatchEnemy.EnemyType.Upd

            dgr = Me.DataGridView1.Rows.Item(Me.DataGridView1.SelectedCells.Item(0).RowIndex)

            we.txtFileName.Text = dgr.Cells(0).Value
            we.cbCreate.Checked = dgr.Cells(1).Value
            we.txtCreateMessage.Text = dgr.Cells(2).Value
            we.cbRename.Checked = dgr.Cells(3).Value
            we.txtRenameMessage.Text = dgr.Cells(4).Value
            we.cbDelete.Checked = dgr.Cells(5).Value
            we.txtDeleteMessage.Text = dgr.Cells(6).Value
            we.cbChange.Checked = dgr.Cells(7).Value
            we.txtChangeMessage.Text = dgr.Cells(8).Value


            we.ShowDialog()
            If Not "".Equals(we.bone.FileName) And Not we.bone.FileName Is Nothing Then


                dgr.Cells(0).Value = we.bone.FileName
                dgr.Cells(1).Value = we.bone.NeedMonitorCreate
                dgr.Cells(2).Value = we.bone.CreateMessage
                dgr.Cells(3).Value = we.bone.NeedMonitorRename
                dgr.Cells(4).Value = we.bone.RenameMessage
                dgr.Cells(5).Value = we.bone.NeedMonitorDelete
                dgr.Cells(6).Value = we.bone.DeleteMessage
                dgr.Cells(7).Value = we.bone.NeedMonitorChange
                dgr.Cells(8).Value = we.bone.ChangeMessage

                MessageBox.Show("Target:[ " & we.bone.FileName & " ] has been updated!")
            End If
        End If


    End Sub

    Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
        If FolderBrowserDialog1.ShowDialog(Me) = DialogResult.OK Then
            Me.txtWatchFolder.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.isSaved = False
        Me.Close()
    End Sub
End Class